﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 目标标本：4.0.30319.239
 * 
 * 最后修改：2011/12/6 13:16:39
 * ************************************************************/
namespace Mozlite.Web.Controls
{
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections.Generic;
    using System.Web.Mvc;
    using System.Linq.Expressions;
    using Mozlite.Common;
    using System.Web.Helpers;
    using System.Web.Mvc.Html;

    /// <summary>
    /// 下啦列表扩展方法类。
    /// </summary>
    public static class DropDownListExtensions
    {
        /// <summary>
        /// 论坛权限方案列表。
        /// </summary>
        /// <typeparam name="TModel">模型类型。</typeparam>
        /// <param name="helper">Html辅助类实例对象。</param>
        /// <param name="expression">表达式。</param>
        /// <param name="attributes">属性。</param>
        /// <returns>返回呈现的字符串。</returns>
        public static MvcHtmlString ForumPermissionSchemaDropDownListFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, Guid>> expression, object attributes = null)
        {
            IList<SelectListItem> items = new List<SelectListItem>();
            foreach (var schema in Permissions.LoadPermissionSchemas<ForumPermission>().Values)
            {
                items.Add(new SelectListItem { Text = schema.SchemaName, Value = schema.SchemaID.ToString() });
            }
            return helper.DropDownListFor(expression, items, attributes);
        }

        #region forums
        /// <summary>
        /// 论坛下拉列表框。
        /// </summary>
        /// <typeparam name="TModel">模型类型。</typeparam>
        /// <param name="helper">Html辅助类实例对象。</param>
        /// <param name="expression">表达式。</param>
        /// <param name="ignoreID">忽略的文件夹ID。</param>
        /// <param name="attributes">属性。</param>
        /// <returns>返回呈现的字符串。</returns>
        public static MvcHtmlString DropDownListForumFor<TModel>(this HtmlHelper<TModel> helper, Expression<Func<TModel, int>> expression, int ignoreID = 0, object attributes = null)
        {
            var items = new List<SelectListItem>();
            items.Add(new SelectListItem { Text = Resources.GetString("Forum.Root"), Value = "0" });
            foreach (var forum in Forums.LoadForums().Values)
            {
                if (forum.ForumID == ignoreID)
                    continue;
                items.Add(new SelectListItem { Text = "├─" + forum.ForumName, Value = forum.ForumID.ToString() });
                AppendSelectListItems(items, forum, 1, ignoreID);
            }
            return helper.DropDownListFor(expression, items, attributes);
        }

        private static void AppendSelectListItems(List<SelectListItem> items, Forum forum, int level = 0, int ignoreID = 0)
        {
            foreach (var child in forum.Children)
            {
                if (child.ForumID == ignoreID)
                    continue;
                items.Add(new SelectListItem { Text = "├─" + "──".Times(level) + child.ForumName, Value = child.ForumID.ToString() });
                if (forum.Children != null)
                    AppendSelectListItems(items, child, level + 1, ignoreID);
            }
        }
        #endregion
    }
}